namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class Login
    {
        public void AddLogin(TSL.Model.Login model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_Login_ADD");
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, model.username);
            db.AddInParameter(dbCommand, "logtime", DbType.AnsiString, model.logtime);
            db.AddInParameter(dbCommand, "logip", DbType.AnsiString, model.logip);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "browser", DbType.AnsiString, model.browser);
            db.AddInParameter(dbCommand, "usertype", DbType.AnsiString, model.usertype);
            db.AddInParameter(dbCommand, "panykey", DbType.AnsiString, model.panykey);
            db.ExecuteNonQuery(dbCommand);
        }

        public void Delete(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_Login_Delete");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            db.ExecuteNonQuery(dbCommand);
        }

        public void DeleteLogin(string ID)
        {
            List<string> strList = new List<string>();
            strList.Add("delete from Login where ID = '" + ID + "'");
            DbHelperSQL.ExecuteSqlTran(strList);
        }

        public int ExecuteSql(string sql)
        {
            return DbHelperSQL.ExecuteSql(sql);
        }

        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Login");
            strSql.Append(" where lottery_num=@name ");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@name", SqlDbType.VarChar, 50) };
            parameters[0].Value = name;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(string ssc_id, string lottery_num)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Login");
            strSql.Append(" where lottery_num=@lottery_num and ssc_id=@ssc_id");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ssc_id", SqlDbType.VarChar, 50), new SqlParameter("@lottery_num", SqlDbType.VarChar, 50) };
            parameters[0].Value = ssc_id;
            parameters[1].Value = lottery_num;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public List<TSL.Model.Login> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_Login ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id asc");
            List<TSL.Model.Login> list = new List<TSL.Model.Login>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_Login ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.Login GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Login_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.Login model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.Login GetModel(string name, string password)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Login_GetModel");
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, name);
            db.AddInParameter(dbCommand, "password", DbType.AnsiString, password);
            TSL.Model.Login model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.Login ReaderBind(IDataReader dataReader)
        {
            TSL.Model.Login model = new TSL.Model.Login();
            model.id = dataReader["id"].ToString();
            model.username = dataReader["username"].ToString();
            model.logtime = dataReader["logtime"].ToString();
            model.logip = dataReader["logip"].ToString();
            model.state = dataReader["state"].ToString();
            model.browser = dataReader["browser"].ToString();
            model.usertype = dataReader["usertype"].ToString();
            model.panykey = dataReader["panykey"].ToString();
            return model;
        }

        public void Update(TSL.Model.Login model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Login_Update");
            db.ExecuteNonQuery(dbCommand);
        }
    }
}

